package com.by.controller;

import com.by.constant.Result;
import com.by.pojo.ShopCart;
import com.by.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/4/16 20:25
 * @注释
 */
@RestController
@RequestMapping("/api")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车接口
     *
     * @param userId
     * @param shopCart
     * @return
     */
    @PostMapping("/add/{userId}")
    public Result<String> addCar(@PathVariable("userId") String userId, @RequestBody ShopCart shopCart) {
        cartService.add(userId, shopCart);
        return Result.success();
    }

    @PutMapping("/update/{userId}")
    public Result<String> updateCart(@PathVariable("userId") String userId, @RequestParam("productId") String productId, @RequestParam("number") String number) {
        cartService.upCart(userId, productId, number);
        return Result.success();
    }

    /**
     * 查询某用户购物车商品信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserCart")
    public Result<Map<String,String>> getUserCart(@RequestParam("userId") String userId) {
        Map<String,String> userCart = cartService.getUserCart(userId);
        return userCart != null ? Result.success(userCart) : Result.failure(400, "查询失败");
    }

    /**
     * 删除购物车某个商品
     * @param userId
     * @param productId
     * @return
     */
    @DeleteMapping("/delShopCart")
    public Result<String>delShopCar(@RequestParam("userId")String userId,@RequestParam("productId")String productId){
        return cartService.delShopCart(userId,productId)>0?Result.success():Result.failure(400,"删除失败");
    }
}
